import { Inject, Controller, Post, Files, File } from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { AttachService } from "../service/att.service";
import { ApiTags } from "@midwayjs/swagger";
import { Utils } from "../utils/index";
import { UploadFileInfo } from "@midwayjs/upload";

@ApiTags("附件")
@Controller()
export class AttachController {
  @Inject()
  ctx: Context;

  @Inject()
  attService: AttachService;

  @Inject()
  utils: Utils;

  @Post("/auth-api/att/cos/single-upload")
  async singleCosUpload(@File() file: UploadFileInfo<any>) {
    if (!file) {
      return { success: false, message: "上传文件不能为空", data: null };
    }
    return await this.attService.putCosFileObject(file);
  }

  @Post("/app-api/att/single-upload", { summary: "单文件上传" })
  async app_single_upload(@File() file: UploadFileInfo<any>) {
    if (!file) {
      return this.utils.response.fail({ message: "上传文件不能为空" });
    }

    const res = await this.attService.upload([file]);

    return this.utils.response.success({
      ...res,
      data: res.data[0]
    });
  }

  @Post("/app-api/att/multiple-upload", { summary: "上传多附件" })
  async app_multiple_upload(@Files() files: Array<UploadFileInfo<any>>) {
    if (!files || files.length === 0) {
      return this.utils.response.fail({ message: "上传文件不能为空" });
    }

    return this.utils.response.success(await this.attService.upload(files));
  }

  @Post("/auth-api/att/single-upload", { summary: "单文件上传" })
  async single_upload(@File() file: UploadFileInfo<any>) {
    if (!file) {
      return this.utils.response.fail({ message: "上传文件不能为空" });
    }

    const res = await this.attService.upload([file]);

    return this.utils.response.success({
      ...res,
      data: res.data[0]
    });
  }

  @Post("/auth-api/att/multiple-upload", { summary: "上传多附件" })
  async multiple_upload(@Files() files: Array<UploadFileInfo<any>>) {
    if (!files || files.length === 0) {
      return this.utils.response.fail({ message: "上传文件不能为空" });
    }

    return this.utils.response.success(await this.attService.upload(files));
  }
}
